package cn.com.homeyoda.aimate.aihealth.ui.questionnaire;

import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.SUBMIT_ANSWER;
import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.SUBMIT_QUESTIONNAIRE_ANSWER;

import android.app.Application;

import androidx.annotation.NonNull;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.utils.KLog;
import com.wzq.mvvmsmart.utils.RxUtils;

import java.util.ArrayList;
import java.util.List;

import cn.com.homeyoda.aimate.aihealth.base.BaseViewModel;
import cn.com.homeyoda.aimate.aihealth.bean.BaseResponse2;
import cn.com.homeyoda.aimate.aihealth.bean.QuestionnaireAnswerBean;
import cn.com.homeyoda.aimate.aihealth.bean.QuestionnaireTopicBean;
import cn.com.homeyoda.aimate.aihealth.bean.QuestionnaireOptionBean;
import cn.com.homeyoda.aimate.aihealth.data.Repository;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class QuestionnaireTopicViewModel extends BaseViewModel<Repository> {

    public final StateLiveData<List<QuestionnaireTopicBean>> questionnaireTopicData;
    public final StateLiveData<List<QuestionnaireOptionBean>> questionnaireOptionData;

    public QuestionnaireTopicViewModel(@NonNull Application application, Repository model) {
        super(application, model);
        questionnaireTopicData = new StateLiveData<>();
        questionnaireTopicData.setValue(new ArrayList<>());

        questionnaireOptionData = new StateLiveData<>();
        questionnaireOptionData.setValue(new ArrayList<>());
    }

    public void getQuestionnaireTopicData(String id) {
        model.getQuestionnaireTopic(id)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(QuestionnaireTopicViewModel.this)
                .subscribe(new Observer<BaseResponse2<List<QuestionnaireTopicBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        questionnaireTopicData.postLoading();
                    }

                    @Override
                    public void onNext(BaseResponse2<List<QuestionnaireTopicBean>> response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            questionnaireTopicData.postValueAndSuccess(response.getData());
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        questionnaireTopicData.postError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void getQuestionnaireOptionData(String id) {
        model.getQuestionnaireOption(id)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(QuestionnaireTopicViewModel.this)
                .subscribe(new Observer<BaseResponse2<List<QuestionnaireOptionBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        questionnaireOptionData.postLoading();
                    }

                    @Override
                    public void onNext(BaseResponse2<List<QuestionnaireOptionBean>> response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            questionnaireOptionData.postValueAndSuccess(response.getData());
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        questionnaireOptionData.postError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void submitQuestionnaireAnswer(QuestionnaireAnswerBean answerBean) {
        model.submitQuestionnaireAnswer(answerBean)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(QuestionnaireTopicViewModel.this)
                .subscribe(new Observer<BaseResponse2>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(BaseResponse2 response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            LiveEventBus.get(SUBMIT_QUESTIONNAIRE_ANSWER).post(true);
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }
}
